package online.siwu.memorycards

import java.io.*


/**
 * Created on 2017/5/29.
 * GlobalData
 * 单例模式
 */
object GlobalData {

    var exerNum = 0
    var jiTime = 0
    var yiTime = 0


    var arrCode = arrayOfNulls<String>(110)
    var arrExer = emptyArray<Int?>()
    var arrAnswer = emptyArray<Int?>()


    // 解读文件
    fun getCode110(file: File) {
        val fr = FileReader(file)
        val bffr = BufferedReader(fr)
        var str: String?

        while (true) {
            str = bffr.readLine()
            if (str == null) break
            linePut(str)
        }
    }

    // 解读一行的内容
    private fun linePut(strline: String) {

        val str = strline.trim()
        val i: Int
        val key: String
        val value: String

        if (str == "") return
        if (str.substring(0, 1) == ";") return
        if (!str.contains('=')) return

        key = getKey(str)
        value = getValue(str)
        if (key == "" || value == "") return

        i = key.toInt()
        if (i < 0 || i > 109) return

        arrCode[i] = value
    }

    // 得到 = 之前部分
    private fun getKey(str: String): String {
        if (str.indexOf("=") > -1)
            return str.substring(0, str.indexOf("=")).trim()
        return ""
    }

    // 得到 = 之后部分
    private fun getValue(str: String): String {
        if (str.indexOf("=") > -1)
            return str.substring(str.indexOf("=") + 1).trim()
        return ""
    }


    // 写文件 form array
    fun writeFile(file: File){

        val fw = FileWriter(file)
        val bufw = BufferedWriter(fw)

        for (i in 0..109){
            bufw.write("$i=${arrCode[i]}")
            bufw.newLine()
            bufw.newLine()
        }

        bufw.close()
    }


    // Int 转 String
    fun int2str110(i: Int): String {
        var now = ""
        if (i < 0 || i > 110) return "-1"
        if (i < 100) now = i.toString()
        when (i) {
            100 -> now = "00"
            101 -> now = "01"
            102 -> now = "02"
            103 -> now = "03"
            104 -> now = "04"
            105 -> now = "05"
            106 -> now = "06"
            107 -> now = "07"
            108 -> now = "08"
            109 -> now = "09"
        }
        return now
    }

    // String 转 Int
    fun str2Int(str: String): Int {
        return when (str) {
            "" -> -1
            "00" -> 100
            "01" -> 101
            "02" -> 102
            "03" -> 103
            "04" -> 104
            "05" -> 105
            "06" -> 106
            "07" -> 107
            "08" -> 108
            "09" -> 109
            else -> str.toInt()
        }
    }

    // 检测文件是否存在
    fun fileExists(f: File): Boolean {
        try {
            if (!f.exists()) return false
        } catch (e: Exception) {
            return false
        }
        return true
    }

    // COPY 文件
    fun copy2Data(file: File, inputStream: InputStream) {
        val bos = BufferedOutputStream(FileOutputStream(file))
        val bis = BufferedInputStream(inputStream)
        var count: Int
        while (true) {
            count = bis.read()
            if (count == -1) break
            bos.write(count)
        }
        bos.close()
        bis.close()
    }

}
